home *** CD-ROM | disk | FTP | other *** search
- Path: gate.itron.com!usenet
- From: ronald.ten-hove@itron.com (Ron Ten-Hove)
- Newsgroups: comp.lang.c++
- Subject: Re: Two way communication between objects
- Date: 31 Jan 1996 20:44:41 GMT
- Organization: Itron Inc.
- Message-ID: <4eokbp$1gk@gate.itron.com>
- References: <310ACCE2.2600@werple.mira.net.au>
- Reply-To: ronald.ten-hove@itron.com (Ron Ten-Hove)
- NNTP-Posting-Host: itron12-23.itron.com
- X-Newsreader: IBM NewsReader/2 v1.02
-
- In <310ACCE2.2600@werple.mira.net.au>, Ross Forder <erosco@werple.mira.net.au> writes:
- >I have to apologise what what is probably a stupid question but I'm only
- >a beginner at some of this stuff.
- >
- >I am trying to write a VERY simple client and server set of objects. I
- >would like to client to register with the server at ctor time and have
- >the server know about the client by saving a pointer to the client
- >so he can callback to a method called say 'event'.
-
- I assume that you are using the terms ``client'' and ``server'' in
- a generic sense, and you are *not* trying to implement a client/
- server system across a communications network or using some
- other IPC mechanism.
-
- >The problem is the fact that they will both need a pointer to each other
- >and this seems impossible using strong typing. Is there a simple 'object
- >oriented' way to do this?
-
- myFile.h
- class Client; // forward
- class Server {
- public:
- virtual void EventHandler( int eventType, Client & theClient );
- enum { RegisterClient, DeregisterClient, LastEvent };
- };
-
- class Client {
- protected:
- Server & itsServer;
- public:
- Client( Server & theServer ) : itsServer( theServer )
- {
- itsServer.EventHandler( Server :: RegisterClient, *this );
- }
- virtual ~Client()
- {
- itsServer.EventHandler( Server :: DeregisterClient, *this );
- }
- virtual void EventHandler( int event, Server & theServer );
- };
-
- >I have been able to make this work by having the server only know about
- >a parent class of the client (say eventhandler) but this is still not a
- >bulletproof solution.
-
- Two things to keep in mind:
-
- 1. Separate source & header files for *declaring* and
- *implementing* classes.
- 2. Use polymorphism. The server doesn't need to know
- much about clients, and vice versa, as long as they
- conform to a protocol. In the example above, the
- protocol is implemented using the virtual function
- EventHandler in both Client and Server.
-
- Good luck!
- -Ron
-